import {ConfigEnv, loadEnv, UserConfig} from "vite";
import uni from "@dcloudio/vite-plugin-uni";

const CWD = process.cwd();

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, CWD);

  const { VITE_WEB_BASE_PATH, VITE_SERVER_BASE_URL, VITE_APP_PORT, VITE_PROXY_SERVER } = env;
  const apiRouteReg = new RegExp(`^${VITE_SERVER_BASE_URL}`);
  // 代理配置
  const proxy = {
    [VITE_SERVER_BASE_URL]: {
      target: VITE_PROXY_SERVER,
      changeOrigin: true,
      ws: true,
      rewrite: (path: string) => path.replace(apiRouteReg, ''),
    },
  };

  return {
    plugins: [uni()],
    // base: VITE_WEB_BASE_PATH, // uniapp 基于 manifest.json 的配置
    server: {
      port: VITE_APP_PORT || 8080,
      proxy
    }
  }
}
